package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.ExamResultMapper;
import com.think.teaching.model.ExamResult;
import com.think.teaching.model.QuestionSelection;
import com.think.teaching.service.ExamResultService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 学生考试结果 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class ExamResultServiceImpl extends BaseServiceImpl<ExamResultMapper, ExamResult> implements ExamResultService {

	@Override
	public ExamResult AddExamResult(ExamResult examResult) {
		if (this.save(examResult)) {
			return examResult;
		}
		return null;
	}


	@Override
	public ExamResult UpdateExamResult(ExamResult examResult) {
		if (this.updateById(examResult)) {
			return examResult;
		}
		return null;
	}

	@Override
	public ExamResult GetExamResult(Integer id) {
		QueryWrapper<ExamResult> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}

	@Override
	public IPage<ExamResult> ListExamResultWithTagAndType(Integer count, Integer page, Integer type, Integer tag,
			Boolean withoutBan) {
		QueryWrapper<ExamResult> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).ne("type", type).ne("tag", tag);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		IPage<ExamResult> ipage = new Page<>(page, count);
		return this.page(ipage);
	}

	@Override
	public List<ExamResult> SearchExamResultWithKey(String key, Boolean withoutBan) {
		QueryWrapper<ExamResult> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).like("answer", key);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return list(queryWrapper);
	}

	@Override
	public ExamResult SetExamResultState(Integer id, Integer state) {
		UpdateWrapper<ExamResult> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}

	/**
	 * 删除和指定的questionId相关的选项
	 * @param questionId
	 * @return
	 */
	public Boolean DeleteRecordsByQuestionId(Integer questionId) {
		UpdateWrapper<ExamResult> queryWrapper = new UpdateWrapper<ExamResult>();
		queryWrapper.eq("question_id", questionId).set("delflag", 1);
		return this.update(queryWrapper);
	}


}
